cmake_minimum_required(VERSION 3.10)

#-----------------------------------------------------------------------
# 工程相关设置
#-----------------------------------------------------------------------
# 设置工程名
project(FUNCSIGN)

# 设置可执行文件名
set(EXE_NAME signfunc)

# 设置 c++17 标准
set(CMAKE_CXX_STANDARD 17)

# 设置编译类型 Debug(0)/Release(1)
# set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_BUILD_TYPE Release)

# 设置输出文件夹
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/Debug/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/Debug/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/Debug/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/Release/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/Release/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/Release/lib)

# 工程include搜索路径
set(PROJ_INC_DIR "")
# 工程lib搜索路径
set(PROJ_LINK_DIR "")
# 工程链接库名
set(PROJ_LINK_LIB "")
# 工程编译选项
set(PROJ_COMP_OPT "")

#-----------------------------------------------------------------------
# 编译文件设置
#-----------------------------------------------------------------------
# 工程目录结构
# HS_XMLPARSER
# └─ src 存放源文件及头文件
set(PROJ_SRC "${PROJECT_SOURCE_DIR}/src")
set(PROJ_INC "${PROJECT_SOURCE_DIR}/inc")

# 获取 src 下的源文件
set(PROJ_SRC_FILES)
file(GLOB_RECURSE PROJ_SRC_FILES ${PROJ_SRC}/*.cpp)
list(APPEND PROJ_SRC_FILES ${PROJECT_SOURCE_DIR}/main.cpp)

# 获取 inc 下的头文件
set(PROJ_INC_FILES)
file(GLOB_RECURSE PROJ_INC_FILES ${PROJ_INC}/*.h)

# 添加到工程include搜索路径
list(APPEND PROJ_INC_DIR ${PROJ_INC})

# 设置第三方库搜索路径和链接库名
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    # windows 下的第三方库路径设置
    message(STATUS "The current system: ${CMAKE_SYSTEM_NAME}")
    
    # easyloggingpp
    set(EASYLOGGINGPP_INC_PATH "${PROJECT_SOURCE_DIR}/thirdparty/easyloggingpp")
    list(APPEND PROJ_INC_DIR ${EASYLOGGINGPP_INC_PATH})
    
    # jsoncpp
    set(JSON_INC_PATH "${PROJECT_SOURCE_DIR}/thirdparty/jsoncpp")
    list(APPEND PROJ_SRC_FILES ${JSON_INC_PATH}/jsoncpp.cpp) # 需要编译源文件
    list(APPEND PROJ_INC_DIR ${JSON_INC_PATH})

    # MSVC的编译选项(不然报错)
    if(MSVC)
        # 解决VS编译中文报错 error C2001:常量中有换行符
        list(APPEND PROJ_COMP_OPT "/utf-8")
        # 解决error C2872: “byte”: 不明确的符号
        list(APPEND PROJ_COMP_OPT "/D_HAS_STD_BYTE=0")
        # sscanf 警告
        list(APPEND PROJ_COMP_OPT "-D_CRT_SECURE_NO_WARNINGS")
    endif()
else() 
    # linux 下的第三方库路径
    message(STATUS "The current system: ${CMAKE_SYSTEM_NAME}")

    # easyloggingpp
    set(EASYLOGGINGPP_INC_PATH "${PROJECT_SOURCE_DIR}/thirdparty/easyloggingpp")
    list(APPEND PROJ_INC_DIR ${EASYLOGGINGPP_INC_PATH})
    
    # jsoncpp
    set(JSON_INC_PATH "${PROJECT_SOURCE_DIR}/thirdparty/jsoncpp")
    list(APPEND PROJ_SRC_FILES ${JSON_INC_PATH}/jsoncpp.cpp) # 需要编译源文件
    list(APPEND PROJ_INC_DIR ${JSON_INC_PATH})
endif()


message(STATUS "Source files: ${PROJ_SRC_FILES}")
message(STATUS "Header files: ${PROJ_INC_FILES}")

#-----------------------------------------------------------------------
# 编译目标
#-----------------------------------------------------------------------
add_executable(${EXE_NAME} ${PROJ_SRC_FILES} ${PROJ_INC_FILES})
target_include_directories(${EXE_NAME} PRIVATE ${PROJ_INC_DIR})
target_link_directories(${EXE_NAME} PRIVATE ${PROJ_LINK_DIR})
target_link_libraries(${EXE_NAME} PRIVATE ${PROJ_LINK_LIB})
target_compile_options(${EXE_NAME} PRIVATE ${PROJ_COMP_OPT})

#-----------------------------------------------------------------------
# 安装相关设置
#-----------------------------------------------------------------------
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/dst)
install(TARGETS ${EXE_NAME} DESTINATION .)
